﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevTeam;
using System;
using System.Data;

namespace ConfExpert.Forms.Aksesuar.Satinalma
{
    public partial class TalepOlustur : XPopupForm
    {
        public string AksPOIds { get; set; }
        private bool IsManuel { get; set; }
        public TalepOlustur(int brandId, int sezonId, string xml, string OrderRef, bool IsManuel)
        {
            InitializeComponent();
            this.BrandId = brandId;
            this.SezonId = sezonId;
            this.Xml = xml;
            this.OrderRef = OrderRef;
            this.IsManuel = IsManuel;
        }

        private void StokTransfer_Load(object sender, EventArgs e)
        {
            btnTalepOlustur.Click += new EventHandler(btnTransfer_Click);
            chkIdle.CheckedChanged += new EventHandler(chkIdle_CheckedChanged);

            GetDetails("SODetay", "GetOrderRefByBrand",BrandId);

            gleOrderRef.Properties.Assign(GetIntellisensedRI(ds.SODetay));
            gleOrderRef.Properties.View.Columns["BrandId"].ColumnEdit = RI(Rp.Brand);
            gleOrderRef.Properties.View.Columns["SezonId"].ColumnEdit = RI(Rp.Sezon);

            gleBrand.Properties.Assign(RI(Rp.Brand));
            gleSezon.Properties.Assign(RI(Rp.Sezon));

            gleBrand.EditValue = BrandId;
            gleSezon.EditValue = SezonId;
            gleOrderRef.EditValue = OrderRef;
            chkIdle.Checked = BrandId == 47;

            gleOrderRef.Enabled = chkIdle.Enabled = gleOrderRef.Text.IsNullOrWhiteSpace() && BrandId != 47;
            gleBrand.Enabled = BrandId.ToInt() == 0;
            gleSezon.Enabled = SezonId.ToInt() == 0;

            gleOrderRef.Properties.ProcessNewValue += new ProcessNewValueEventHandler(ri_ProcessNewValue2);
            gleOrderRef.EditValueChanged += new EventHandler(gleOrderRef_EditValueChanged);
            cmbTercih.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            cmbTercih.SelectedIndexChanged += new EventHandler(cmbTercih_SelectedIndexChanged);
            this.Shown += new EventHandler(Transfer_Shown);
        }

        void gleOrderRef_EditValueChanged(object sender, EventArgs e)
        {
            if (gleOrderRef.Properties.View.GetFocusedRow() != null)
            {		 
                DataRow dr = ((DataRowView)gleOrderRef.Properties.View.GetFocusedRow()).Row;
                gleBrand.EditValue = intParse(dr["BrandId"]);
                gleSezon.EditValue = intParse(dr["SezonId"]);
            }
        }

        void cmbTercih_SelectedIndexChanged(object sender, EventArgs e)
        {
            deTill.Visible = cmbTercih.SelectedIndex == 2;
            cmbSure.Visible = cmbTercih.SelectedIndex == 1;
        }

        void Transfer_Shown(object sender, EventArgs e)
        {
            gleOrderRef.Focus();
        }


        void chkIdle_CheckedChanged(object sender, EventArgs e)
        {
            gleOrderRef.Enabled = !chkIdle.Checked;
            if (chkIdle.Checked)
                gleOrderRef.EditValue = null;
        }


        public int AyrimId = 0;
        public int BrandId = 0;
        public int SezonId = 0;
        public string OrderRef = string.Empty;
        private string Xml;
        void btnTransfer_Click(object sender, EventArgs e)
        {
            string name = gleOrderRef.EditValue.ToStr();
            if (name.IsNullOrWhiteSpace() && !chkIdle.Checked)
            {
                XtraMessageBox.Show("OrderRef bilgisi girin ya da IDLE kutucuğunu işaretleyin !");
                return;
            }
            if (intParse(gleBrand.EditValue) == 0)
            {
                XtraMessageBox.Show("Marka boş olamaz!");
                return;
            }
            if (intParse(gleSezon.EditValue) == 0)
            {
                XtraMessageBox.Show("Sezon boş olamaz!");
                return;
            }

            DataTable AksPO = ExecuteDataTable("InsAksesuarPO", WhoAmI, gleOrderRef.EditValue, gleBrand.EditValue, gleSezon.EditValue, Xml, cbKesinlestir.Checked, IsManuel);
            AksPOIds = AksPO.GetValuesofSpecifiedColumn("AksPOId", ",");
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}